package com.cxd.app.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cxd.app.entity.base.UserPointBase;
import com.cxd.app.service.BeanService;
import com.cxd.app.service.PointService;
import com.cxd.app.service.UserService;
import com.cxd.base.common.constants.UserPointHistoryConstants;
import com.cxd.base.common.exception.CxdException;
import com.cxd.base.controller.BaseController;
import com.cxd.base.util.BeanTools;
import com.cxd.base.util.MapUtil;
import com.cxd.base.util.ParamUtil;

/**
 * <一句话功能简述> <功能详细描述>
 * @author wtj
 * @date 2016年4月18日 下午12:08:01
 * @company CXD
 * @version [Copyright (c) 2016 V001]
 */
@Controller("PointController")
@RequestMapping("pointc")
public class PointController  extends BaseController{
	
	@Resource
	public BeanTools bt;
	
	@Resource
	public BeanService beanService;
	
	@Resource
	public PointService pointService;
	
	@Resource
	public UserService userService;
	
	/**
	 * 获取用户总积分
	 * @author wtj
	 * @date 2016年5月11日上午9:52:46
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "getUserPoint", method = {RequestMethod.GET,RequestMethod.POST })
	public void getUserPoint(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
				Map<String, Object> param = MapUtil.jsonToMap(value);
				Object userId = param.get("userId");
				ParamUtil.notNullParam("用户ID:"+userId);
				UserPointBase up = beanService.getUserPointByUserId(userId.toString());
				Map<String,Object> mr = new HashMap<String,Object>();
				if(up!=null&&up.getPoint()!=null){
					mr.put("point", up.getPoint());
					json.put("result",mr);
				}else{
					mr.put("point", "0");
					json.put("result",mr);
				}
				if(json.get("status")==null){
					this.jsonMsg(json, 200, "获取用户总积分成功");
				}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/**
	 * 获取积分历史记录 (积分明细)
	 * @author wtj
	 * @date 2016年5月16日上午11:12:14
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "getUserPointHistory", method = {RequestMethod.GET,RequestMethod.POST })
	public void getUserPointHistory(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("getUserPointHistory",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			pointService.getUserPointHistory(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/* 礼品列表 */
	@RequestMapping(value = "giftRecommend", method = {RequestMethod.GET,RequestMethod.POST })
	public void giftRecommend(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("giftRecommend",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			pointService.giftRecommend(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/* 礼品详情 */
	@RequestMapping(value = "giftDetails", method = {RequestMethod.GET,RequestMethod.POST })
	public void giftDetails(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("giftDetails",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			pointService.giftDetails(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/* 获取用户收货地址 */
	@RequestMapping(value = "getUserAddress", method = {RequestMethod.GET,RequestMethod.POST })
	public void getUserAddress(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("getUserAddress",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			pointService.getUserAddress(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	/* 保存用户收货地址 */
	@RequestMapping(value = "saveUserAddress", method = {RequestMethod.GET,RequestMethod.POST })
	public void saveUserAddress(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("saveUserAddress",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
				pointService.saveUserAddress(json, value);
				if(json.get("status")==null){
					this.jsonMsg(json, 200, "成功");
				}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/* 获取用户抽奖记录 */
	@RequestMapping(value = "getUserPrizeDetails", method = {RequestMethod.GET,RequestMethod.POST })
	public void getUserPrizeDetails(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("getUserPrizeDetails",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			pointService.getUserPointHistory(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	
	/* 获取投资信息 */
	@RequestMapping(value = "getInvestByInvestId", method = {RequestMethod.GET,RequestMethod.POST })
	public void getInvestByInvestId(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("getInvestByInvestId",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			pointService.getInvestByInvestId(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/* uphID获取产品发货信息 */
	@RequestMapping(value = "getProductByUphId", method = {RequestMethod.GET,RequestMethod.POST })
	public void getProductByProductId(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("getProductByUphId",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			pointService.getProductByUphId(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/* 保存兑换的商品 */
	@RequestMapping(value = "saveDHGift", method = {RequestMethod.GET,RequestMethod.POST })
	public void saveDHGift(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("saveDHGift",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
				pointService.saveDHGift(json, value);
				if(json.get("status")==null){
					this.jsonMsg(json, 200, "成功");
				}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/* 积分抽奖H5 页面 */
	@RequestMapping(value = "pointLuckDraw", method = {RequestMethod.GET,RequestMethod.POST })
	public void pointLuckDraw(HttpServletRequest request,HttpServletResponse response,String user_id)throws IOException {
		Map<String, Object> root = new HashMap<String,Object>();
//		if(!userService.checkToken(request)){
//			JSONObject json = new JSONObject();
//			this.jsonMsg(json, 403, "用户未登录");
//			super.responseSendMsg(request,response,json);
//		}else{
//			Object  token = request.getHeader("TOKEN");
//			root.put("token", token);
			this.writeTemplate(response, root, "/tpl/lottery_app_two.html");
//		}
		
	}
	
	
	
	/* 用户积分抽奖信息 */
	@RequestMapping(value = "user_point_prize", method = {RequestMethod.GET,RequestMethod.POST })
	public void user_can_prize_info(HttpServletRequest request,HttpServletResponse response,String userId)throws IOException {
		super.requestLog("user_point_prize",userId);
		JSONObject json = new JSONObject();
		try {
				UserPointBase ub = bt.queryBean("user_point", "user_id", userId, UserPointBase.class);
				json.put("integral_total", ub.getPoint());
				json.put("can_prize", ub.getPoint()/UserPointHistoryConstants.prizeOne);
				if(json.get("status")==null){
					this.jsonMsg(json, 200, "成功");
				}
		} catch(CxdException e){
			super.cxdJsonException(json, e,userId);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(request,response,json);
	}
	
	/* 保存抽奖操作  */
	@RequestMapping(value = "saveLuckDraw", method = {RequestMethod.GET,RequestMethod.POST })
	public void saveLuckDraw(HttpServletRequest request,HttpServletResponse response,String userId)throws IOException{
		super.requestLog("saveLuckDraw",userId);
		JSONObject json = new JSONObject();
		try {
				pointService.saveLuckDraw(json, userId);
				if(json.get("status")==null){
					this.jsonMsg(json, 200, "成功");
				}
		} catch(CxdException e){
			super.cxdJsonException(json, e,userId);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(request,response,json);
	}
	
}
